package com.security.door.controller.calendarManage;

import com.security.door.core.wrapper.WrapMapper;
import com.security.door.core.wrapper.Wrapper;
import com.security.door.model.domain.calendarSetting;
import com.security.door.model.dto.calendarSettingDto;
import com.security.door.service.IcalendarSettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.UUID;

@RestController
@Api(value = "WEB - calendarSettingController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class calendarSettingController {

    @Autowired
    HttpSession session;
    @Autowired
    HttpServletRequest httpServletRequest;
    @Autowired
    IcalendarSettingService icalendarSettingService;

    @PostMapping("/calendar/selectListByDate")
    @ApiOperation(value = "根据时间查询设置信息", notes = "根据时间查询设置信息")
    public Wrapper deleteByGuid(@RequestBody calendarSettingDto entityDto) {
        var list = icalendarSettingService.selectListByDate(entityDto);
        return WrapMapper.ok(list);
    }

    @GetMapping("/calendar/queryListWithPage")
    @ApiOperation(value = "分页查询所有信息", notes = "分页查询所有信息")
    public Wrapper queryListWithPage() {
        calendarSettingDto entityDto = new calendarSettingDto();
        int pageIndex = httpServletRequest.getParameter("start") == null ? 0 : Integer.parseInt(httpServletRequest.getParameter("start"));
        int pageSize = httpServletRequest.getParameter("length") == null ? Integer.MAX_VALUE : Integer.parseInt(httpServletRequest.getParameter("length"));
        String searchStr = httpServletRequest.getParameter("search[value]");
        entityDto.setKeyWord(searchStr);
        entityDto.setPageIndex(pageIndex / pageSize + 1);
        entityDto.setPageSize(pageSize);

        var list = icalendarSettingService.queryListWithPage(entityDto);
        return WrapMapper.ok(list);
    }

    @PostMapping("/calendar/addWorkDay")
    public Wrapper addWorkDay(@RequestBody calendarSetting entity) {
        var tempEntity = icalendarSettingService.selectByDate(entity);
        if (null == tempEntity) {
            entity.setGuid(UUID.randomUUID().toString());
            entity.setCreatetime(new Date());
            icalendarSettingService.save(entity);
        } else {
            tempEntity.setWorkDay(entity.getWorkDay());
            icalendarSettingService.update(tempEntity);
        }
        return WrapMapper.ok(entity);
    }

    @GetMapping("/calendar/cancelWorkDay/{attDate}")
    public Wrapper cancelWorkDay(@PathVariable("attDate") String attDate) {
        var result = icalendarSettingService.cancelWorkDay(attDate);
        return WrapMapper.ok(result);
    }

    @PostMapping("/calendar/addWorkTime")
    public Wrapper addWorkTime(@RequestBody calendarSetting entity) {
        if (null != entity) {
            if (null == entity.getGuid() || "".equals(entity.getGuid())) {
                entity.setGuid(UUID.randomUUID().toString());
                if (null == entity.getAmOn() || entity.getAmOn().isBlank())
                    entity.setAmOn(null);
                if (null == entity.getAmOff() || entity.getAmOff().isBlank())
                    entity.setAmOff(null);
                if (null == entity.getPmOn() || entity.getPmOn().isBlank())
                    entity.setPmOn(null);
                if (null == entity.getPmOff() || entity.getPmOff().isBlank())
                    entity.setPmOff(null);
                if (null == entity.getAmEarliest() || entity.getAmEarliest().isBlank())
                    entity.setAmEarliest(null);
                if (null == entity.getPmEarliest() || entity.getPmEarliest().isBlank())
                    entity.setPmEarliest(null);
                entity.setCreatetime(new Date());
                entity.setGlobalSet(true);
                icalendarSettingService.save(entity);
            } else {
                calendarSetting tempEntity = icalendarSettingService.selectByKey(entity.getGuid());
                tempEntity.setTitle(entity.getTitle());
                if (null != entity.getAmOn() && !entity.getAmOn().isBlank())
                    tempEntity.setAmOn(entity.getAmOn());
                if (null != entity.getAmOff() && !entity.getAmOff().isBlank())
                    tempEntity.setAmOff(entity.getAmOff());
                if (null != entity.getPmOn() && !entity.getPmOn().isBlank())
                    tempEntity.setPmOn(entity.getPmOn());
                if (null != entity.getPmOff() && !entity.getPmOff().isBlank())
                    tempEntity.setPmOff(entity.getPmOff());
                if (null != entity.getAmEarliest() && !entity.getAmEarliest().isBlank())
                    tempEntity.setAmEarliest(entity.getAmEarliest());
                if (null != entity.getPmEarliest() && !entity.getPmEarliest().isBlank())
                    tempEntity.setPmEarliest(entity.getPmEarliest());
                tempEntity.setLateTime(entity.getLateTime());
                tempEntity.setAbsentTime(entity.getAbsentTime());
                tempEntity.setGlobalSet(true);
                icalendarSettingService.update(tempEntity);
            }
        }
        return WrapMapper.ok(entity);
    }

    @GetMapping("/calendar/delWorkTime/{guids}")
    public Wrapper delWorkTime(@PathVariable("guids") String guids) {
        icalendarSettingService.deleteByIds(guids);
        return WrapMapper.ok();
    }
}
